﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace mklib.json
{
	/// <summary>
	/// 数组容器
	/// </summary>
	internal class JsonValueArray : IEnumerable, IJsonValue
	{
		
		internal JsonValueArray(int depth)
		{
			this._Depth = depth;
			data = new List<IJsonValue> ();
		}

		private readonly List<IJsonValue> data;
		internal IEnumerable<IJsonValue> Data
		{
			get
			{
				return data;
			}
		}

		internal void Add(IJsonValue val)
		{
			data.Add (val);
		}

		#region IJsonValue implementation

		private readonly int _Depth;
		int IJsonValue.Depth
		{
			get
			{
				return _Depth;
			}
		}

		JsonValueType IJsonValue.Type
		{
			get
			{
				return JsonValueType.Array;
			}
		}

		object IJsonValue.ValueOf ()
		{
			object[] vals = new object[data.Count];
			int i = 0;
			foreach (IJsonValue val in data) {
				vals [i++] = val.ValueOf ();
			}
			return vals;

		}
		void IJsonValue.Each(JsonEachMethod m)
		{
			int i = 0, c = this.data.Count;
			IJsonValue val;
			for (; i < c; i++) {
				val = this.data [i];
				switch (val.Type) {
				case JsonValueType.Array:
				case JsonValueType.Object:
					m (i, null, val.Type, _Depth);
					val.Each (m);
					break;
				default:
					m (i, val.ValueOf (), val.Type, val.Depth);
					break;
				}
			}
		}

		#endregion

		#region IEnumerable implementation

		IEnumerator IEnumerable.GetEnumerator ()
		{
			return data.GetEnumerator ();
		}

		#endregion

	}
}

